﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using QLQTPM_ThuDuc_Group7.DTO;
using DesignLayOut_QLQTPM_Team7.DAO;


namespace QLQTPM_ThuDuc_Group7.DAO
{
    class TenDiaDiemDAO
    {
        //--------Delete----------
        public static bool XoaTenDiaDiem(int matendiadiem)
        {
            bool kq = true;
            DataProvider dp = new DataProvider();
            dp.connect();
            string sql = "delete from TenDiaDiem where MaTenDiaDiem=" + matendiadiem;
            kq = dp.executeNonQuery(sql);
            dp.disconnect();
            if (kq == false)
                return kq;
            return kq;
        }
        
        public static bool KiemTraXoaTenDiaDiem(int matendiadiem)
        {
            bool flag = true;
            DataProvider dp = new DataProvider();
            dp.connect();
            string sql = "select * from TenDiaDiem where MaTenDiaDiem=" + matendiadiem;
            DataTable dt = new DataTable();
            dt = dp.executeQuery(sql);
            dp.disconnect();
            int count = dt.Rows.Count;
            if (count == 0)
                flag = false;
            return flag;
        }
        //--------Kết thúc Delete----------

        public int ThemTenDiaDiem(DTO.TenDiaDiemDTO tendiadiem)
        {
            int id = 0;
            DataProvider dp = new DataProvider();
            try
            {
                
                dp.connect();
                string sql = "Insert into tendiadiem(tendiadiem) values ('" + tendiadiem.TenDiaDiem + "')";
                //id = (int)dp.executeScalar(sql);
                bool kq = dp.executeNonQuery(sql);
                if (kq)
                {
                    ////Lấy ra giá trị mã vừa thêm được
                    //sql = "SELECT @@IDENTITY as ID";
                    //OleDbDataReader reader = dp.executeReader(sql);
                    //if (reader.Read())
                    //    id = (int)reader.GetValue(reader.GetOrdinal("ID"));
                }

            }
            catch (OleDbException ex)
            {
                //Log4Net
                Console.WriteLine(ex.Message);
            }
            finally
            {
                dp.disconnect();
            }
            return id;
        }
        //-------Bat dau Update------
        public static DataTable LoadDiaDiem()
        {
            string sql = "select * from TENDIADIEM";
            DataProvider dtb = new DataProvider();
            return dtb.executeQuery(sql);
        }

        public static bool SuaTenDiaDiem(TenDiaDiemDTO tdd)
        {
            DataProvider dtb = new DataProvider();
            string sql = "";
            sql = "update TENDIADIEM set ";
            sql += "TenDiaDiem ='" + tdd.TenDiaDiem + "',";

            sql += " where MaTenDiaDiem =" + tdd.MaTenDiaDiem;

            return dtb.executeNonQuery(sql);
        }
        //-------Kết thúc Update------

        //-------Bat dau Insert------
        public static bool ThemMoiTenDiaDiem(string info)
        {
            DataProvider dtb = new DataProvider();
            string sql = "";
            sql = "Insert into TENDIADIEM(TenDiaDiem) values('" + info + "')";
            return dtb.executeNonQuery(sql);
        }
        //-------Kết thúc Insert------

        public static bool TonTai(string info)
        {
            DataProvider dtb = new DataProvider();
            string sql = "";
            sql = "Select * from TENDIADIEM where TenDiaDiem ='" + info + "'";
            if (dtb.executeQuery(sql).Rows.Count != 0)
            {
                return true;
            }
            return false;
        }
    }
}
